package com.yy.job.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThrowableUtils {

	private static final Logger log = LoggerFactory.getLogger(ThrowableUtils.class);

	public static String getStackTrace(Throwable throwable) {
		try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
			throwable.printStackTrace(pw);
			return sw.toString();
		} catch (Exception ex) {
			log.error("ThrowableUtils#getStackTrace failed", ex);
			return "print stack trace error, please check";
		}
	}

	public static String subStackTrace(String input, int length) {
		if (StringUtils.isNotBlank(input) && input.length() > length) {
			return input.substring(0, length);
		}

		return input;
	}

	public static String subStackTrace(String input) {
		return subStackTrace(input, 4000);
	}

}
